﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NP.Model;

namespace NP.BL
{
    public class CRUD
    {
        public void ReadCustomers()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                var customers = db.Customers.ToArray();

                int[] l = { 
                          customers.Max(n => n.CustomerID.ToString().Length), 
                          customers.Max(n => n.CompanyName.ToString().Length),
                          customers.Max(n => n.City.ToString().Length),
                          customers.Max(n => n.Country.ToString().Length)
                          //customers.Max(n => n.Phone.ToString().Length)
                      };

                int m = l.Length + 7;

                StringBuilder sb = new StringBuilder();

                foreach (var i in customers)
                {
                    sb.Append('-', l.Sum() + m);
                    sb.AppendLine();
                    sb.AppendFormat("| {0,-" + l[0] + "} |", i.CustomerID);
                    sb.AppendFormat(" {0,-" + l[1] + "}|", i.CompanyName);
                    sb.AppendFormat(" {0,-" + l[2] + "}|", i.City);
                    sb.AppendFormat(" {0,-" + l[3] + "} |", i.Country);
                    //sb.AppendFormat("{0,-" + l[3] + "}|", i.Phone);
                    sb.AppendLine();
                }
                sb.Append('-', l.Sum() + m);
                sb.AppendLine();
                Console.Write(sb.ToString());
            }
        }

        public void UpdateCustomer()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                Customer cust = null;

                do
                {
                    Console.WriteLine("Podaj ID klienta");
                    string custid = Console.ReadLine();

                    cust = db.Customers.ToArray().SingleOrDefault(n => n.CustomerID.Equals(custid.ToUpper()));
                    if (cust == null)
                        Console.WriteLine("ID nie istnieje!");
                }
                while (cust == null);

                do
                {
                    Console.WriteLine("Podaj nazwę klienta");
                    cust.CompanyName = Console.ReadLine();
                    if (cust.CompanyName == "")
                        Console.WriteLine("Pole nie może być puste!");
                }
                while (cust.CompanyName == "");

                Console.WriteLine("Osoba kontaktowa");
                cust.ContactName = Console.ReadLine();
                Console.WriteLine("Stanowisko osoby kontaktowej");
                cust.ContactTitle = Console.ReadLine();
                Console.WriteLine("Ulica i numer");
                cust.Address = Console.ReadLine();
                Console.WriteLine("Miasto");
                cust.City = Console.ReadLine();
                Console.WriteLine("Kod pocztowy");
                cust.PostalCode = Console.ReadLine();
                Console.WriteLine("Region");
                cust.Region = Console.ReadLine();
                Console.WriteLine("Country");
                cust.Country = Console.ReadLine();
                Console.WriteLine("Telefon");
                cust.Phone = Console.ReadLine();
                Console.WriteLine("Fax");
                cust.Fax = Console.ReadLine();

                try
                {
                    db.SubmitChanges();
                    Console.WriteLine("Poprawnie zmodyfikowano klienta");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Wystąpił błąd spróbuj ponownie.");
                }
            }
        }

        public void NewData()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                Customer c = new Customer();
                int cid = 0;
                do
                {
                    Console.WriteLine("Podaj ID (max 5 znaków)");
                    c.CustomerID = Console.ReadLine().ToUpper();
                    if (c.CustomerID.Length <= 5)
                    {
                        cid = db.Customers.Where(n => n.CustomerID == c.CustomerID).Count();
                        if (cid > 0 || c.CustomerID == "")
                        {
                            Console.WriteLine("Podaj unikalne ID klienta!");
                        }
                    }
                    else
                        Console.WriteLine("ID jest zadługie");
                }
                while (cid > 0 || c.CustomerID == "" || c.CustomerID.Length > 5);

                do
                {
                    Console.WriteLine("Podaj nazwę klienta");
                    c.CompanyName = Console.ReadLine();
                    if (c.CompanyName == "")
                        Console.WriteLine("Pole nie może być puste!");
                } 
                while (c.CompanyName == "");

                Console.WriteLine("Osoba kontaktowa");
                c.ContactName = Console.ReadLine();
                Console.WriteLine("Stanowisko osoby kontaktowej");
                c.ContactTitle = Console.ReadLine();
                Console.WriteLine("Ulica i numer");
                c.Address = Console.ReadLine();
                Console.WriteLine("Miasto");
                c.City = Console.ReadLine();
                Console.WriteLine("Kod pocztowy");
                c.PostalCode = Console.ReadLine();
                Console.WriteLine("Region");
                c.Region = Console.ReadLine();
                Console.WriteLine("Country");
                c.Country = Console.ReadLine();
                Console.WriteLine("Telefon");
                c.Phone = Console.ReadLine();
                Console.WriteLine("Fax");
                c.Fax = Console.ReadLine();

                try
                {
                    db.Customers.InsertOnSubmit(c);
                    db.SubmitChanges();
                    Console.WriteLine("Popraawnie wprowadzono klienta.");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Wystąpił błąd spróbuj ponownie.");
                }
            }
        }

        public void DeleteCustomer()
        {
            using (var db = new NorthwindDataClassesDataContext())
            {
                Customer cust = null;
                do
                {
                    Console.WriteLine("Podaj ID klienta do usunięcia");
                    string custid = Console.ReadLine();

                    cust = db.Customers.ToArray().SingleOrDefault(n => n.CustomerID.Equals(custid.ToUpper()));
                    if (cust == null)
                        Console.WriteLine("ID nie istnieje!");
                }
                while (cust == null);

                try
                {
                    db.Customers.DeleteOnSubmit(cust);
                    db.SubmitChanges();

                    Console.WriteLine("Poprawnie usunięto klienta");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Wystąpił błąd spróbuj ponownie.");
                }
            }
        }

    }
}
